from django.contrib.auth.models import User
from rest_framework import status, viewsets
from rest_framework.decorators import action, renderer_classes
from rest_framework.generics import RetrieveAPIView
from rest_framework.parsers import FileUploadParser
from rest_framework import renderers
from rest_framework.views import APIView
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.reverse import reverse

from snippets.models import Album, Track
from snippets.serializers import UserSerializer, CreateUserSerializer, AlbumSerializer, TrackSerializer

# from snippets.serializers import AlbumSerializer, TrackSerializer


@api_view(['GET'])
def api_root(request, format=None):

    return Response({
        'users': reverse('user-list', request=request, format=format),
        'snippets': reverse('snippet-list', request=request, format=format)
    })



class AlbumViewSet(viewsets.ModelViewSet):

    queryset = Album.objects.all()

    serializer_class = AlbumSerializer


class TrackViewSet(viewsets.ModelViewSet):

    queryset = Track.objects.all()

    serializer_class = TrackSerializer


class UserViewSet(viewsets.ModelViewSet):

    queryset = User.objects.all()
    serializer_class = CreateUserSerializer

    # renderer_classes = [renderers.JSONRenderer]


    def list(self, request, *args, **kwargs):

        serializer = self.get_serializer()

        return super(UserViewSet, self).list(request, *args, **kwargs)



    @api_view(['GET'])
    @renderer_classes([renderers.JSONRenderer])
    def user_count_view(self, request, format=None):

        user_count = User.objects.filter(is_active=True).count()

        content = {'user_count': user_count}


        return Response(content)


class UserDetail(RetrieveAPIView):

    queryset = User.objects.all()
    serializer_class = CreateUserSerializer

    # renderer_classes = [renderers.TemplateHTMLRenderer]

    def get(self, request, *args, **kwargs):

        self.object = self.get_object()

        return Response({'user': self.object}, template_name='user_detial.html')


    @api_view(['GET'])
    @renderer_classes([renderers.StaticHTMLRenderer])
    def simple_html_view(self, request):

        data = '<html><body><h1>Hello, world</h1></body></html>'

        return Response(data)


class FileUploadView(APIView):

    parser_classes = [FileUploadParser]

    def put(self, request, filename, format=None):

        file_obj = request.data['file']


        return Response(status=204)



class TestView(APIView):


    def get(self, request, *args, **kwargs):

        msg = 'hehehehe'

        return Response(msg)



